



model{

for (i in 1:n2){ ### Loop over n2 who completed the BGM


## model outcome responses 

fol15d[i]~dbern(p7.bound[i]) # Have ideas (internal efficacy)
p7.bound[i]<-max(0, min(1, p7[i]))
p7[i]<-phi(constant[1] + b.treatment[1]*treatment[i] + b.kn[1]*kn[i] + b.genknowd[1]*genknowd[i] + b.base[1]*bas15d[i] + 1*eta1[i] # + (lambda.eta1r[1] + b.hetero[1]*treatment[i])*eta1[i] 
+ b.white[1]*white[i] + b.female[1]*female[i] + b.work[1]*work[i])

fol13d[i]~dbern(p5.bound[i]) # Officials care (external efficacy)
p5.bound[i]<-max(0, min(1, p5[i]))
p5[i]<-phi(constant[2] + b.treatment[2]*treatment[i] + b.kn[2]*kn[i] + b.genknowd[2]*genknowd[i]  + b.base[2]*bas13d[i] + lambda.eta1[1]*eta1[i] # + (lambda.eta1r[2] + b.hetero[2]*treatment[i])*eta1[i]  
+ b.white[2]*white[i] + b.female[2]*female[i] + b.work[2]*work[i])



## model compliance indicators (measurement model for compliance type, or eta1):


discpart[i]~dbern(p11.bound[i]) # complied with the treatment
p11.bound[i]<-max(0, min(1, p11[i]))
p11[i]<-phi(constant[3]  + lambda.eta1[2]*eta1[i] + b.kn[3]*kn[i] + b.genknowd[3]*genknowd[i] + b.white[3]*white[i] + b.female[3]*female[i] + b.work[3]*work[i])  # b.asstre*asstre[i] + + b.asstre*treatprop[i] 

bas15d[i]~dbern(p14.bound[i]) # pretreatment internal efficacy
p14.bound[i]<-max(0, min(1, p14[i]))
p14[i]<-phi(constant[4] + lambda.eta1[3]*eta1[i] + b.kn[4]*kn[i] + b.genknowd[4]*genknowd[i] + b.white[4]*white[i] + b.female[4]*female[i] + b.work[4]*work[i])

bas13d[i]~dbern(p12.bound[i]) # pretreatment external efficacy
p12.bound[i]<-max(0, min(1, p12[i]))
p12[i]<-phi(constant[5] + lambda.eta1[4]*eta1[i] + b.kn[5]*kn[i] + b.genknowd[5]*genknowd[i] + b.white[5]*white[i] + b.female[5]*female[i] + b.work[5]*work[i])

follcomp[i]~dbern(p16.bound[i]) # completed the follow up survey
p16.bound[i]<-max(0, min(1, p16[i]))
p16[i]<-phi(constant[6] + lambda.eta1[5]*eta1[i]*3 + b.kn[6]*kn[i] + b.genknowd[6]*genknowd[i] + b.white[6]*white[i] + b.female[6]*female[i] + b.work[6]*work[i]) 

novcomp[i]~dbern(p17.bound[i]) # completed the November survey
p17.bound[i]<-max(0, min(1, p17[i]))
p17[i]<-phi(constant[7] + b.novelig*novelig[i] + lambda.eta1[6]*eta1[i] + b.kn[7]*kn[i] + b.genknowd[7]*genknowd[i] + b.white[7]*white[i] + b.female[7]*female[i] + b.work[7]*work[i])
# for the novcomp equation, we handle eligibility by conditioning on novelig, which is endogenous and a function of treatment and follcomp, which are conditionally independent

}



## distribution for compliance type (eta1) 
for (j in 1:n2){
eta1[j]~dnorm(mu1.cd[j],1)I(-4,4)
mu1.cd[j]<-mu.eta1*march[j]
# this conditions compliance type on when the session occurred, summer of 2006 =0 and March 2007 = 1
}


##### PRIORS FOR PARAMETERS

###### the model works better if you include restrictions on factor coefficient signs:
#for (m in 1:6) {
#lambda.eta1[m]~dunif(0,4)
#}

# but we ran it with non-directional priors: 
for (m in 1:6) {
lambda.eta1[m]~dnorm(0.0,0.001)I(-5,5)
}

mu.eta1~dnorm(0.0,0.001)I(-4,4)
b.novelig~dnorm(0.0,0.001)I(-5,5)

for (h in 1:7) {
constant[h]~dnorm(0.0,0.001)I(-5,5)
}

for (h in 1:7) {
b.kn[h]~dnorm(0.0,0.001)I(-5,5)
b.genknowd[h]~dnorm(0.0,0.001)I(-5,5)
b.white[h]~dnorm(0.0,0.001)I(-5,5)
b.female[h]~dnorm(0.0,0.001)I(-5,5)
b.work[h]~dnorm(0.0,0.001)I(-5,5)
}

for (h in 1:2) {
b.base[h]~dnorm(0.0,0.001)I(-5,5)
b.treatment[h]~dnorm(0.0,0.001)I(-5,5)
#b.hetero[h]~dnorm(0.0,0.001)I(-5,5)
}


### end
}


